<?php

namespace models\DAO;

use models\Exception\DAO\ModelDriverException;
use models\Exception\DAO\ModelException;
use models\Exception\DAO\ModelReflectionException;
use models\Exception\DAO\ModelSqlException;


/**
 * @SWG\Swagger(
 *     @SWG\Definition(
 *          type="object",
 *          definition="bookingSingleData",
 *          required={"errno", "errmsg", "data"},
 *          @SWG\Property(property="errno", type="integer", format="int32", description="编码"),
 *          @SWG\Property(property="errmsg", type="string", format="string", description="错误提示信息"),
 *          @SWG\Property(property="result", type="object", ref="#/definitions/bookingSingleObj")
 *     ),
 *     @SWG\Definition(
 *          type="object",
 *          definition="bookingSingleObj",
 *          @SWG\Property(property="data", type="object", ref="#/definitions/bookingSingle")
 *     ),
 *     @SWG\Definition(
 *          type="object",
 *          definition="bookingListData",
 *          required={"errno", "errmsg", "result"},
 *          @SWG\Property(property="errno", type="integer", format="int32", description="编码"),
 *          @SWG\Property(property="errmsg", type="string", format="string", description="错误提示信息"),
 *          @SWG\Property(property="result", ref="#/definitions/bookingListObj"),
 *     ),
 *     @SWG\Definition(
 *          type="object",
 *          definition="bookingListObj",
 *          @SWG\Property(property="data", type="object", ref="#/definitions/bookingData"),
 *          @SWG\Property(property="meta", ref="#/definitions/meta")
 *     ),
 *     @SWG\Definition(
 *          type="array",
 *          definition="bookingData",
 *          @SWG\Items(
 *              title="data", ref="#/definitions/bookingSingle"
 *          )
 *     ),
 *     @SWG\Definition(
 *          type="object",
 *          definition="bookingSingle",
 *          @SWG\Property(property="sn", type="string", format="string",  description="订购信息编号"),
 *          @SWG\Property(property="trade_order_sn", type="string", format="string",  description="上游订单号"),
 *          @SWG\Property(property="member_id", type="integer", format="int32",  description="会员编号"),
 *          @SWG\Property(property="product_id", type="string", format="string",  description="商品编号"),
 *          @SWG\Property(property="sku_id", type="string", format="string",  description="阿里巴巴skuId"),
 *          @SWG\Property(property="address_id", type="integer", format="int32",  description="收货地址"),
 *          @SWG\Property(property="num", type="integer", format="int32",  description="商品数量"),
 *          @SWG\Property(property="price", type="string", format="string",  description="价格(向用户收取的费用,包含运费)"),
 *          @SWG\Property(property="fee", type="string", format="string",  description="运费"),
 *          @SWG\Property(property="stats", type="integer", format="int32",  description="状态(0为未支付,1为已支付,2为发起退款,3为已退款,4为发起退货,5为已退货,9为拼团成功,10拼团失败,6为确认收货)"),
 *          @SWG\Property(property="channel", type="integer", format="int32",  description="支付通道(0为支付宝,1为微信)"),
 *          @SWG\Property(property="created_at", type="string", format="string",  description="创建时间"),
 *          @SWG\Property(property="expired_at", type="string", format="string",  description="过期时间"),
 *          @SWG\Property(property="refund_at", type="string", format="string",  description="退款发起时间"),
 *          @SWG\Property(property="return_at", type="string", format="string",  description="退货发起时间"),
 *          @SWG\Property(property="updated_at", type="string", format="string",  description="最后一次更新时间"),
 *          @SWG\Property(property="product_info", type="object", format="object", description="参考product/info的返回结果")
 *     )
 * )
 */
class BookingModel extends BaseModel{
    /**
     * 订购信息编号
     */
    public $sn = null;

    /**
     * 上游订单号
     */
    public $trade_order_sn = null;

    /**
     * 会员编号
     */
    public $member_id = null;

    /**
     * 商品编号
     */
    public $product_id = null;

    /**
     * 阿里巴巴skuId
     */
    public $sku_id = null;

    /**
     * 收货地址
     */
    public $address_id = null;

    /**
     * 商品数量
     */
    public $num = null;

    /**
     * 价格(向用户收取的费用,包含运费)
     */
    public $price = null;

    /**
     * 运费
     */
    public $fee = null;

    /**
     * 状态(0为未支付,1为已支付,2为发起退款,3为已退款,4为发起退货,5为已退货,9为拼团成功,10拼团失败,6为确认收货)
     */
    public $stats = null;

    /**
     * 支付通道(0为支付宝,1为微信)
     */
    public $channel = null;

    /**
     * 创建时间
     */
    public $created_at = null;

    /**
     * 过期时间
     */
    public $expired_at = null;

    /**
     * 退款发起时间
     */
    public $refund_at = null;

    /**
     * 退货发起时间
     */
    public $return_at = null;

    /**
     * 最后一次更新时间
     */
    public $updated_at = null;
    /**
     * DAO对象
     */
    public $obj = '';

    /**
     * DAO对象数组
     */
    public $data = [];

    /**
     * 翻页对象
     */
    public $meta = null;

    /**
     * 当前页
     */
    public $page = 1;

    /**
     * 每页显示条数
     */
    public $page_size = 10;

    /**
     * 主键数组
     */
    public $primary_key_arr = ["sn"];

    /**
     * 自增字段数组
     */
    public $auto_increment_key_arr = [];

    /**
     * 唯一字段
     */
    public $unique_key_arr = '';

    /**
     * 单例实例
     */
    static public $instance = null;

    /**
     * @return BookingModel|null
     * @throws ModelSqlException
     */
    static public function getInstance() {
        if(is_null(self::$instance)){
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * BookingModel constructor.
     * @throws ModelSqlException
     */
    public function __construct() {
        parent::__construct(str_replace("Model", "", substr(get_class($this), strrpos(get_class($this), "\\")+1)));
        $this->meta = new \stdClass();
    }

    /**
     * @throws ModelException
     */
    public function __clone() {
        throw new ModelException(
            str_replace('%s', get_class($this), ModelException::INSTANCE_NOT_ALLOW_TO_CLONE), ModelException::INSTANCE_NOT_ALLOW_TO_CLONE_NO);
    }

    public function __destruct() {
        self::$instance = null;
    }

    /**
     * set属性
     *
     * __set.
     * @param string $name 参数名
     * @param string $value 参数值
     * @return $this
     */
    public function __set($name = "", $value = "") {
        $this->$name = $value;
        return $this;
    }

    /**
     * get属性
     *
     * __get.
     * @param string $name 参数名
     * @return mixed
     */
    public function __get($name = "") {
        return  $this->$name ;
    }

    /**
     * 创建bookingModel
     *
     * @return mixed
     * @throws ModelDriverException
     * @throws ModelReflectionException
     * @throws ModelSqlException
     * @throws ModelException
     */
    public function insert() {
        return $this->insertRecord();
    }

    /**
     * 编辑bookingModel
     *
     * @return mixed
     * @throws ModelDriverException
     * @throws ModelReflectionException
     * @throws ModelSqlException
     * @throws ModelException
     */
    public function update() {
        return $this->updateRecord();
    }

    /**
     * 删除bookingModel，需先指定主键
     *
     * @return mixed
     * @throws ModelDriverException
     * @throws ModelReflectionException
     * @throws ModelSqlException
     * @throws ModelException
     */
    public function delete() {
        return $this->deleteRecord();
    }

    /**
     * 根据主键查询bookingModel，获取单条记录
     *
     * find.
     * @param int $primary_key 主键编号
     * @return $this
     * @throws ModelException
     * @throws ModelReflectionException
     */
    public function find($primary_key = 0) {
        $result = $this->findByPrimaryKey($primary_key);

        $this->setModelProperty($result);

        return $this;
    }

    /**
     * 根据条件数组查询bookingModel，获取多条记录
     *
     * findOneBy.
     * @param array $condition 查询条件
     * @return $this
     * @throws ModelDriverException
     * @throws ModelException
     * @throws ModelReflectionException
     * @throws ModelSqlException
     */
    public function findOneBy($condition = []) {
        $result = $this->findRecordBy($condition);

        $this->setModelProperty($result);

        return $this;
    }

    /**
     * 根据条件数组查询bookingModel，获取多条记录
     *
     * findBy.
     * @param array $condition 查询条件
     * @return $this
     * @throws ModelDriverException
     * @throws ModelException
     * @throws ModelReflectionException
     * @throws ModelSqlException
     */
    public function findBy($condition = []) {
        $count = $this->getCount($condition);
        $total = $count->fetchColumn();

        $this->data = [];
        $book_obj = $this->findRecordBy($condition, $this->page, $this->page_size);

        $this->genMeta($total);
        $this->setModelProperty($book_obj, true);

        return $this;
    }

    /**
     * 批量插入bookingModel
     *
     * @return mixed
     * @throws ModelDriverException
     * @throws ModelException
     * @throws ModelSqlException
     */
    public function batchInsert() {
        return $this->batchInsertRecord();
    }

    /**
     * 批量删除bookingModel
     *
     * @throws ModelDriverException
     * @throws ModelException
     * @throws ModelSqlException
     */
    public function batchDelete() {
        $this->batchDeleteRecord();
    }

    /**
     * 生成批量生成update的sql
     *
     * @return bool|string
     * @throws ModelDriverException
     * @throws ModelException
     * @throws ModelSqlException
     */
    public function genBatchUpdateSql() {
        return $this->_genBatchUpdateSql();
    }

}
